package 算法.无限极乘法;

import java.util.Arrays;

/**
 * @author coderFu
 * @create 2022/3/19
 * 两个String 相加
 */
public class CoderAddString {

    public static void main(String[] args) {
        String add = add("123456789", "2");
        System.out.println(add);
    }

    public static String add(String s1, String s2){
        if (s1.length() == 0 || s2.length() == 0){
            return  "0";
        }

        int[] arrayS1 = new int[s1.length()];
        int[] arrayS2 = new int[s2.length()];

        for(int i=0; i<s1.length(); i++){
            arrayS1[i] = s1.charAt(i)-'0';
        }

        for(int i=0; i<s2.length(); i++){
            arrayS2[i] = s2.charAt(i)-'0';
        }

        int[] result = new int[s1.length()+s2.length()];

        for(int i=0; i<arrayS1.length; i++){
            for(int j=0; j<arrayS2.length; j++){
                result[i+j] += arrayS1[i]*arrayS2[j];
            }
        }

        for(int k= result.length-1; k>0; k--){
            // 进位
            result[k-1] += result[k]/10;
            // 进位后的结果，就是最后的尾数值
            result[k] = result[k]%10;
        }
        StringBuilder sb = new StringBuilder();
        for(int i=0; i<result.length-1; i++){
            sb.append(result[i]);
        }
        return  sb.toString();
    }

}
